using System;
using System.Linq;
using System.Text;
using HipChat;
using Wolfpack.Core;
using Wolfpack.Core.Interfaces.Entities;

namespace Wolfpack.Contrib.Publishers.HipChat.Formatters
{
    public class CheckFormatter : MessageFormatterBase
    {
        public CheckFormatter(HipChatPublisherConfig config) : base(config)
        {
        }

        public override bool AppliesToNotification(NotificationEvent notification)
        {
            return notification.EventType.Equals(NotificationEventHealthCheck.EventTypeName, 
                StringComparison.OrdinalIgnoreCase);
        }

        public override HipChatMessage Format(HipChatMessage message, NotificationEvent notification)
        {
            SetMessageText(message, notification);
            SetMessageFrom(message, notification);
            SetMessageBackground(message, notification);
            return message;
        }

        protected virtual void SetMessageText(HipChatMessage message, NotificationEvent notification)
        {
            string resultLine1;

            if (!notification.Result.HasValue)
                resultLine1 = "Result is Unknown!";
            else if (notification.Result.Value)
                resultLine1 = "Passed!";
            else
                resultLine1 = "<b>** Failed **</b>";

            string resultLine2;
            string resultLine3 = null;
            string resultLine4 = null;

            if (notification.CriticalFailure)
            {
                resultLine2 = "<b>*** CRITICAL FAILURE ***</b>";
                resultLine3 = string.Format("Wolfpack Log Ref:={0}", notification.CriticalFailureDetails.Id);
                resultLine4 = notification.Message;
            }
            else
            {
                resultLine2 = notification.Message;
            }

            var textBuilder = new StringBuilder(string.Format("{1}{0}{2}{0}", CRLF, notification.CheckId, DateTime.Now));
            textBuilder.AppendIf(!string.IsNullOrEmpty(resultLine1), "{0}{1}", resultLine1, CRLF);
            textBuilder.AppendIf(!string.IsNullOrEmpty(resultLine2), "{0}{1}", resultLine2, CRLF);
            textBuilder.AppendIf(!string.IsNullOrEmpty(resultLine3), "{0}{1}", resultLine3, CRLF);
            textBuilder.AppendIf(!string.IsNullOrEmpty(resultLine4), "{0}{1}", resultLine4, CRLF);

            textBuilder.AppendIf(notification.ResultCount.HasValue, "Count:={0}{1}",
                notification.ResultCount.GetValueOrDefault(), CRLF);

            if (notification.Properties != null)
            {
                notification.Properties.ToList().ForEach(
                    p => textBuilder.AppendFormat("{0}:={1}{2}", p.Key, p.Value, CRLF));
            }

            message.Text = textBuilder.ToString();
        }

        protected virtual void SetMessageBackground(HipChatMessage message, NotificationEvent notification)
        {
            if (notification.Result.HasValue)
            {
                message.BackgroundColor = notification.Result.Value
                                              ? HipChatClient.BackgroundColor.green
                                              : HipChatClient.BackgroundColor.red;
            }
        }

        protected virtual void SetMessageFrom(HipChatMessage message, NotificationEvent notification)
        {
            message.From = GetFrom(notification.AgentId, notification.SiteId);
        }
    }
}